package com.appoint.buss.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.appoint.buss.domain.EpiparaRnaSeq;
import com.appoint.buss.domain.vo.EpiparaRnaRnaSeqRespVO;
import com.appoint.buss.service.AsyncRnaSeqService;
import com.appoint.common.exception.ServiceException;
import com.appoint.common.utils.StringUtils;
import com.appoint.common.utils.http.HttpUtils2;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.appoint.buss.mapper.EpiparaInfoMapper;
import com.appoint.buss.domain.EpiparaRnaInfo;
import com.appoint.buss.service.IEpiparaInfoService;

import javax.annotation.Resource;

/**
 * epipara_infoService业务层处理
 *
 * @author appoint
 * @date 2023-01-10
 */
@Slf4j
@Service
public class EpiparaInfoServiceImpl extends ServiceImpl<EpiparaInfoMapper, EpiparaRnaInfo> implements IEpiparaInfoService{

    @Resource
    private EpiparaInfoMapper epiparaInfoMapper;
    @Resource
    private AsyncRnaSeqService asyncRnaSeqService;
    /**
     * 查询epipara_info
     *
     * @param id epipara_info主键
     * @return epipara_info
     */
    @Override
    public EpiparaRnaInfo selectEpiparaInfoById(Long id) {
        return epiparaInfoMapper.selectEpiparaInfoById(id);
    }

    /**
     * 查询epipara_info列表
     *
     * @param epiparaInfo epipara_info
     * @return epipara_info
     */
    @Override
    public List<EpiparaRnaInfo> selectEpiparaInfoList(EpiparaRnaInfo epiparaInfo) {
        return epiparaInfoMapper.selectEpiparaInfoList(epiparaInfo);
    }

    /**
     * 新增epipara_info
     *
     * @param epiparaInfo epipara_info
     * @return 结果
     */
    @Override
    public long insertEpiparaInfo(EpiparaRnaInfo epiparaInfo) {
        return epiparaInfoMapper.insertEpiparaInfo(epiparaInfo);
    }

    /**
     * 修改epipara_info
     *
     * @param epiparaInfo epipara_info
     * @return 结果
     */
    @Override
    public int updateEpiparaInfo(EpiparaRnaInfo epiparaInfo) {
        return epiparaInfoMapper.updateEpiparaInfo(epiparaInfo);
    }

    /**
     * 批量删除epipara_info
     *
     * @param ids 需要删除的epipara_info主键
     * @return 结果
     */
    @Override
    public int deleteEpiparaInfoByIds(Long[] ids) {
        return epiparaInfoMapper.deleteEpiparaInfoByIds(ids);
    }

    /**
     * 删除epipara_info信息
     *
     * @param id epipara_info主键
     * @return 结果
     */
    @Override
    public int deleteEpiparaInfoById(Long id) {
        return epiparaInfoMapper.deleteEpiparaInfoById(id);
    }

    @Override
    public List<EpiparaRnaRnaSeqRespVO> queryEpipara(EpiparaRnaSeq reqVO) {
        if(!StringUtils.isEmpty(reqVO.getOrderProp())){
            String orderByParam = "order by " + reqVO.getOrderProp();
            if(!StringUtils.isEmpty(reqVO.getOrderParam()) && "descending".equals(reqVO.getOrderParam())){
                orderByParam += " desc ";
            }
            reqVO.setOrderByParam(orderByParam);
        }
        return epiparaInfoMapper.queryEpipara(reqVO);
    }

    @Override
    public String importUser(List<EpiparaRnaInfo> list, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }
        String resultMsg = "恭喜您，数据已全部导入成功！共 " + list.size() + " 条";
        try {
            this.saveBatch(list);
            asyncRnaSeqService.saveRnaSeq(list,"RNA-seq",operName);
        } catch (Exception e) {
            resultMsg  = "很抱歉，导入失败！";
            log.error(resultMsg, e);
        }
        return resultMsg;
    }

    @Override
    public String list4(Map<String,Object> epiparaInfo) {
        return HttpUtils2.sendPost("http://170.106.188.80:18221/search_gene", JSON.toJSONString(epiparaInfo));
    }
}
